//
//  ReHotSongListViewController.m
//  
//
//  Created by dlios on 15-5-25.
//
//

#import "ReHotSongListViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "RecommendLatestModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "RecommendLatestModel.h"
#import "ReNewMusicTableViewCell.h"
#import "PlayMusicViewController.h"

@interface ReHotSongListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) NSMutableDictionary *littleDic;
@property (nonatomic, retain) NSMutableDictionary *littleDic2;
@property (nonatomic, retain) NSMutableArray *picArray;
@property (nonatomic, retain) NSMutableArray *songArray;
@property (nonatomic, copy) NSMutableString *string;
@property (nonatomic,retain) NSMutableArray *songListArray;
@property (nonatomic, retain) NSMutableArray *playUrlArray;
@property (nonatomic, retain) UIImageView *myImageView;
@end

@implementation ReHotSongListViewController
- (void)dealloc
{
    [_tableView release];
    [_littleDic release];
    [_littleDic2 release];
    [_picArray release];
    [_songArray release];
    [_string release];
    [_str release];
    [_playUrlArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.littleDic = [NSMutableDictionary dictionary];
        self.littleDic2 = [NSMutableDictionary dictionary];
        self.picArray = [NSMutableArray array];
        self.songArray = [NSMutableArray array];
        self.string = [NSMutableString string];
        self.songListArray = [NSMutableArray array];
        self.playUrlArray = [NSMutableArray array];
    }
    return self;
}





-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.tableView];
    [self.view bringSubviewToFront:self.headerView];
   
    
    
}







#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 200, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 667 * 200 - self.view.frame.size.height / 667 * 64)];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ReNewMusicTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView release];
}

#pragma mark - 创建tableHeaderView
- (void)createHeaderView
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 150)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 375 * 20, self.view.frame.size.height / 667 * 40, self.view.frame.size.height / 667 * 120, self.view.frame.size.height / 667 * 120)];
    NSString *str = [self.picArray objectAtIndex:0];
    NSURL *imageUrl = [NSURL URLWithString:str];
    [imageView sd_setImageWithURL:imageUrl placeholderImage:nil];
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = self.view.frame.size.height / 667 * 10;
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 375 * 160, self.view.frame.size.height / 667 * 30, self.view.frame.size.height / 667 * 60, self.view.frame.size.height / 667 * 60)];
    NSString *str2 = [self.littleDic2 objectForKey:@"pic"];
    NSURL *imageUrl2 = [NSURL URLWithString:str2];
    imageView2.clipsToBounds = YES;
    imageView2.layer.cornerRadius = self.view.frame.size.height / 667 * 30;
    [imageView2 sd_setImageWithURL:imageUrl2 placeholderImage:nil];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 375 * 225, self.view.frame.size.height / 667 * 50, self.view.frame.size.width / 375 * 150, self.view.frame.size.height / 667 * 20)];
    titleLabel.text = [self.littleDic2 objectForKey:@"nick_name"];
    titleLabel.font = [UIFont systemFontOfSize:self.view.frame.size.height / 667 * 20];
    [_headerView addSubview:titleLabel];
    
    UILabel *introLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 375 * 160, self.view.frame.size.height / 667 * 90, self.view.frame.size.width / 375 * 200, self.view.frame.size.height / 667 * 100)];
    introLabel.text = [self.littleDic objectForKey:@"tweet"];
    introLabel.numberOfLines = 0;
    introLabel.font = [UIFont systemFontOfSize:self.view.frame.size.height / 667 * 12];
    [_headerView addSubview:introLabel];
    
    [_headerView addSubview:imageView2];
    [_headerView addSubview:imageView];
    [self.view addSubview:_headerView];
    
    self.tableView.tableHeaderView = _headerView;
    [_headerView release];
    [imageView release];
    [imageView2 release];
    [titleLabel release];
    [introLabel release];
}

- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    self.navigationItem.title = @"最新专辑";
    [self connect];
    
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -  使用AFNetworking网络连接
- (void)connect
{
    // 解析tableHeaderView的网络数据
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    //    NSString *str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=[97210245]"];
    [m GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dic objectForKey:@"data"];
        self.littleDic = [array objectAtIndex:0];
        self.littleDic2 = [self.littleDic objectForKey:@"user"];
        self.picArray = [self.littleDic objectForKey:@"pics"];
        NSArray *array2 = [self.littleDic objectForKey:@"songlist"];
        static NSString *string = @"http://ting.hotchanson.com/songs/downwhite?utdid=VH621NYGPzQDAMRYgXwVA2E8&song_id=";
        NSMutableString *string3 = [NSMutableString stringWithString:string];
        for (NSMutableDictionary *dic in array2) {
            RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.songArray addObject:model];
            NSString *string2 = [NSString stringWithFormat:@"%@%%2c", model.song_id];
            [string3 appendString:string2];
            self.string = string3;
        }
        [self createHeaderView];
        [self.tableView reloadData];
        
        
        // 解析tableView中的网络数据
        AFHTTPRequestOperationManager *m2 = [AFHTTPRequestOperationManager manager];
        m2.responseSerializer = [AFHTTPResponseSerializer serializer];
        [m2 GET:self.string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSData *data = operation.responseData;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
            
            
            NSMutableArray *array = [dic objectForKey:@"data"];
            
            for (NSMutableDictionary *dic in array) {
                RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.songListArray addObject:model];
                
                // 如果model中的图片链接数组元素大于2个再取，否则会崩溃
                if (model.url_list.count >= 2) {
                    dic2 = model.url_list[1];
                    NSString *url = [dic2 objectForKey:@"url"];
                    [self.playUrlArray addObject:url];
                }
                
            }
            
            [self createTableView];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark - cell中放的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建自定义的cell
    ReNewMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
    cell.backgroundColor = [UIColor clearColor];
    model = [self.songListArray objectAtIndex:indexPath.row];
    //    cell.songName.text = model.song_name;
    NSString *str = [NSString stringWithFormat:@"%ld.%@", indexPath.row + 1, model.song_name];
    cell.songName.text = str;
    cell.songName.textColor = [UIColor whiteColor];
    cell.singer.text = model.singer_name;
    cell.singer.textColor = [UIColor whiteColor];
    return cell;
}

#pragma mark -  获取tableView的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songListArray.count;
}

#pragma mark -  设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.width / 375 * 70;
}

#pragma mark -  点击tableView的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendLatestModel *model = [self.songListArray objectAtIndex:indexPath.row];
    NSString *str = [self.playUrlArray objectAtIndex:indexPath.row];
    PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
    playMusic.songName = model.song_name;
    playMusic.singer = model.singer_name;
    playMusic.songUrl = str;
    playMusic.songArray = self.playUrlArray;
    playMusic.songListArray = self.songListArray;
    playMusic.num = indexPath.row;

    [self.navigationController pushViewController:playMusic animated:YES];
    [playMusic release];
}
@end
